﻿<script type="text/javascript">
    GetJsonSproc("usp_GetDataSets",

         function (json) {

             $("#kstests .ui-widget-content, #dataruns .ui-widget-content").jsontable();

             var sel = $("#DataSetSelect");
             sel.empty();
            // sel.change(UpdateFilters);

             $.each(json, function (key, value) {
                 console.dir(value);
                 var o = $("<option>").text(value.Name).val(value.ID);
                 sel.append(o);
             });
           //  UpdateFilters();
         });

    var dataRuns, ksTests;
    var dataSetId;

    function GenerateRuns()
    {
              $("#ProgressLog").empty().hide();
            var form = $("#form");
            form.find("input._required").each( function ()
            {
                var t = $(this);
                t.toggleClass("ui-state-error", t.val() == "");
            });

            if(form.find("input.ui-state-error").length > 0)
            {
                return;
            }

            $("#workinganimation").show();
            $("#generaterunsbutton").hide();
            dataSetId = $("#DataSetSelect > option:selected").val();
            var parms = {
                datasetid: dataSetId,
                binstart: $("#binstart").val(),
                binsize: $("#binsize").val(),
                bintarget: $("#bintarget").val(),
                Is3D: $("#Is3D").is(":checked") ? 1 : 0
            }

            if ($("#vmin").val() != "") parms.vmin = $("#vmin").val();
            if ($("#vmax").val() != "") parms.vmax = $("#vmax").val();


           
            GetJsonMultiResultSproc("usp_GetDataRunTable",
                parms,
                function (json)
                {
                    console.dir(json);
                     dataRuns = json.Table;
                     ksTests = json.Table1;

                     $("#dataruns, #kstests").show();

                     $("#dataruns .ui-widget-content").jsontable("option",
                         {
                             source: dataRuns
                         });

                     $("#kstests .ui-widget-content").jsontable("option",
                        {
                            source: ksTests
                        });
                    $("#workinganimation").hide();
                    $("#generaterunsbutton, #queuebutton").show();
                });
            
        }
    var targetToSave;
    var targetKsToSave;
    var savedRuns = 0;
    var savedTests = 0;
    function SaveRuns()
    {
        alert("DO NOT CLOSE THIS WINDOW/TAB UNTIL RUNS ARE QUEUED. Click OK to continue. ");
        $("#generaterunsbutton, #queuebutton").hide();
        $("#dataruns, #kstests").hide();
        progressLog = $("#ProgressLog");
        progressLog.show();
        
        targetToSave = dataRuns.length;
        savedRuns = 0;
        for (var i = 0; i < dataRuns.length; i++)
        {
            var run = dataRuns[i];
            pushRun(run);
        }

    }

    function processTests()
    {
        targetKsToSave = ksTests.length;
        savedTests = 0;
        for (var i = 0; i < ksTests.length; i++)
        {
            var test = ksTests[i];
            pushTest(test);
        }

    
    }

    function pushRun(run)
    {

        var parms =
            {
                UserId: localStorage.UserId,
                DataSetId: $("#DataSetSelect > option:selected").val(),
                Title: encodeURIComponent(run.shortname),
                Distance: run.fitrange,
                SourceFilter: encodeURIComponent(run.primFilter),
                NeighborFilter: encodeURIComponent(run.secFilter),
                Email: 0,
                Pushover: 0,
                Is3d: run.is3d ? 1 : 0,
                vmin: run.vMin,
                vmax: run.vMax
            };

       
      //  console.dir(parms.Is3d);
      //  return;
      
        if (run.vMin == "" || run.vMin == null)
        {
            delete parms.vmin;
            delete parms.vmax;
        }


        GetJsonSproc("usp_AddDataRunRequest",
           parms,
        function(json)
        {
            log("Added run " + run.shortname + " with ID of " + json[0].id);
            runIds[run.shortname] = json[0].id;
            savedRuns++;
            if (savedRuns == targetToSave)
                processTests();
        });

       
    }


    function pushTest(test) 
    {
        console.dir(test);
        var run1 = runIds["" + test.set1];
        var run2 = runIds["" + test.set2];
        //  console.log("run1 " + run1 + " run2 " + run2);
        //  return;
       // return;
        GetJsonSproc("usp_AddKsTestRequest",
            {
                DataRun1: run1,
                DataRun2: run2
            }, function (json)
            {
                savedTests++;
                log("Queue KS test for " + test.set1 + " vs " + test.set2);
                if (savedTests == targetKsToSave)
                {
                    $("#generaterunsbutton").show();
                    alert("All runs and tests saved. You can now leave the page.");
                    RefreshPendingRuns();
                }
            })
    }

    var progressLog;
    var runIds = {};
    

    function log(s)
    {
        progressLog.append("<div>" + s + "</div>");
    }


</script>

<div class="ui-widget mb">
    <div class="ui-widget-header">Generate bulk data runs and KS Tests</div>
    
    <div class="ui-widget-content" id="form">
        Catalog: <select id="DataSetSelect"></select>
        <input type="number" id="binstart" placeholder="Run Start" class="_required" style="width: 5em;" />
        <input type="number" id="binsize" placeholder="Step Size" class="_required" style="width: 5em;" />
        <input type="number" id="bintarget" placeholder="Max Step" class="_required" style="width: 5em;" />
        <input type="checkbox" id="Is3D" /><label for="Is3D">3D?</label>
        <input type="number" id="vmin" placeholder="vmin" style="width: 5em;" />
        <input type="number" id="vmax" placeholder="vmax" style="width: 5em;"/>

        <span id ="generaterunsbutton" class="button" data-icon="fa-table" onclick="GenerateRuns();">Generate Runs</span>
        <span id="workinganimation" style="display:none;"><i class="fa fa-fw fa-spin fa-rotate"></i> Working...</span>
        <span id="queuebutton" class="button" data-icon="fa-save" onclick="SaveRuns();" style="display:none;">Queue Listed Runs and KS Tests</span>
    </div>
</div>
        <div class="ui-widget" id="dataruns" style="float:left; width: 600px;">
            <div class="ui-widget-header">Data Runs</div>
            <div class="ui-widget-content" style="overflow-y: scroll; min-height: 500px; max-height: 500px;"></div>
        </div>

        <div class="ui-widget ml" id="kstests" style="float: left; width: 300px;">
            <div class="ui-widget-header">KS Tests</div>
            <div class="ui-widget-content" style="overflow-y: scroll; min-height: 500px; max-height: 500px;"></div>
        </div>
    
<div id="ProgressLog"></div>
